<?php
/**
 * Organization Controller for RoomBooking Component
 * 
 * @version 1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


class RoomBookingControllerOrganization extends RoomBookingController
{

	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'add' , 'edit' );
		$this->registerTask( 'apply' , 'save' );
	}
	
	/**
	 * display the edit form
	 * @return void
	 */
	function edit()
	{
		JRequest::setVar( 'view', 'organization' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();
	}
	
	/**
	 * save a record and redirect
	 * @return void
	 */	
	function save()
	{
		$organization = JRequest::get('POST');
		if ($organization['name']){
			$model = $this->getModel('organization');

			if ($returnid = $model->store($post)) {
				$msg = JText::_( 'ORGSAVED' );
				$type='message';
			} else {
				$msg = JText::_( 'SAVEORGERROR' );
				$type='error';
			}
		}
		else {
			$msg = JText::_( 'FIELDMISSING' );
			$type='error';
		}
		if ($this->getTask()=='apply')
			$link ='index.php?option=com_roombooking&controller=organization&task=edit&cid='.$returnid;
		else
			$link = 'index.php?option=com_roombooking&view=organizations';
		$this->setRedirect($link, $msg, $type);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove()
	{
		$model = $this->getModel('organization');
		if(!$model->delete()) {
			$msg = JText::_( 'NOORGSDELETE' );
			$type='error';
		} else {
			$msg = JText::_( 'ORGSDELETE' );
			$type='message';
		}

		$this->setRedirect('index.php?option=com_roombooking&view=organizations', $msg, $type );
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'OPERCANC' );
		$this->setRedirect( 'index.php?option=com_roombooking&view=organizations', $msg );
	}
	
}
